const $getQueryVariable = (url) => {
    var params = {}, h;
    var hash = url.slice(url.indexOf("?") + 1).split('&');
    for (var i = 0; i < hash.length; i++) {
        h = hash[i].split("="); //
        params[h[0]] = h[1];
    }
    return params;
}

let routerMethods = {
    components: {},
    data() {
        return {}
    },
    computed: {},
    mounted() {
    },
    beforeRouteLeave(to, from, next) {
        let fullPath = from.meta.isChild ? from.meta.parentPath : this.$route.fullPath
        if (this.pageChange) {
            this.$confirm('页面内容未保存，确定离开吗？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.closePage(fullPath)
                next()
            })
        } else {
            this.closePage(fullPath)
            next()
        }
    },
    methods: {

        /**
         * 动态初始化路由
         */
        initRouterUrl(routerList, router, store) {
            if (!routerList) return
            let self = this
            let r = self.$router
            if (router) r = router
            let systemModules = import.meta.glob("../view/**/*.vue")
            const systemComponents = {};
            Object.keys(systemModules).forEach(key => {
                systemComponents[key.substring(7)] = systemModules[key]
            });
            let modules = import.meta.glob("/src/views/**/*.vue")
            const components = {};
            Object.keys(modules).forEach(key => {
                components[key.substring(10)] = modules[key]
            });
            routerList.forEach(router => {
                let pagePath = router.pagePath
                let path = router.path, query = null
                if (router.pageType === 'page') {
                    pagePath = "/pageTemp/tablePage"
                }
                if (router.pageId && pagePath) {
                    let component = null
                    if (router.noPower) {
                        component = systemComponents['/system/401.vue'];
                    } else if (router.isSystem || router.pageType === 'page') {
                        component = systemComponents[pagePath + '.vue']
                    } else {
                        component = components[pagePath + '.vue']
                    }
                    if (!component) component = systemComponents['/system/404.vue'];
                    if (router.path && router.path.indexOf('?') > -1) {
                        path = router.path.split("?")[0]
                        query = $getQueryVariable(router.path)
                    }
                    if (router.isDefaultHome && store) {
                        store.commit("setDefaultRoutePath", path);
                    }
                    r.addRoute("body", {
                        path: path,
                        name: router.name,
                        component: component,
                        meta: {
                            routerId: router.id,
                            pageId: router.pageId,
                            icon: router.source,
                            newWindow: true,
                            query: query
                        }
                    })
                }
            })
        },

        /**
         * 切换标签栏
         * @param url
         * @param query
         */
        $routeTab(url, query) {
            let getCurrentPagePath = this.$store.getters.getCurrentPagePath
            let routePath = url.substr(0, url.indexOf("?"))
            let currentPath = getCurrentPagePath.substr(0, getCurrentPagePath.indexOf("?"))

            let thatPage = this.$store.getters.getPageByPath(url)
            if (thatPage) {
                if (currentPath === routePath) {
                    this.$store.commit('setPageChange', {
                        fullPath: url
                    })
                }
                this.$routeUrl(thatPage.fullPath || url, query)
            } else {
                this.$routeUrl(url, query)
            }
        },

        /**
         * 页面路由跳转
         * @param url
         * @param query
         * @returns {Promise<Route>}
         */
        $routeUrl(url, query) {
            if (url.indexOf('?') > -1) {
                query = $getQueryVariable(url)
                url = url.split("?")[0]
            }
            // sessionStorage.setItem("search_" + url, "")
            return this.$router.push({path: url, query: query})
        },

        /**
         * 关闭页面
         * @param fullPath
         */
        async $closePage(fullPath) {
            let self = this
            if (!fullPath) fullPath = this.$route.fullPath
            let previousPage = this.$store.getters.getPreviousPage(fullPath)
            self.$store.commit('destroyPage', fullPath)
            if (previousPage) {
                this.$routeTab(previousPage.fullPath, previousPage.query)
            } else {
                this.$routeTab('/')
            }
        },

        /**
         * 切换页面
         * @param url
         */
        $replacePage(url, query) {
            let self = this
            let closePage = this.$route.meta && this.$route.meta.root ? this.$route.path : this.$route.fullPath
            if (closePage) self.$store.commit('destroyPage', closePage)
            this.$routeUrl(url, query)
        },

        /**
         * 重新设置页面参数
         * @param query
         */
        $resetQuery(query) {
            let fullPath = this.$route.meta && this.$route.meta.root ? this.$route.path : this.$route.fullPath
            if (fullPath) this.$store.commit('resetQuery', {
                fullPath: fullPath,
                query: query
            })
        },

        /**
         * 关闭其他页面
         * @param fullPath
         */
        async $closeOtherPage(fullPath) {
            let self = this
            if (!fullPath) fullPath = this.$route.fullPath

            let jumpPage = true
            let pagesList = this.$store.getters.getPagesList
            for (let i = 0; i < pagesList.length; i++) {
                if (pagesList[i].fullPath !== fullPath) {
                    let thatPage = self.$store.getters.getPageByPath(pagesList[i].fullPath)
                    if (thatPage.fullVue && thatPage.fullVue.pageChange) {
                        self.jumpPage = false
                    } else {
                        self.$store.commit('destroyPage', pagesList[i].fullPath)
                        i = -1
                    }
                }
            }
            if (jumpPage) {
                let thatPage = this.$store.getters.getPageByPath(fullPath)
                if (thatPage) {
                    this.$routeTab(thatPage.fullPath, thatPage.query)
                }
            }
        },

        /**
         * 关闭右侧页面
         * @param fullPath
         */
        $closeRightPage(fullPath) {
            let self = this
            if (!fullPath) fullPath = this.$route.fullPath

            let jumpPage = true
            let pagesList = this.$store.getters.getPagesList
            let currentIndex = -1
            for (let i = 0; i < pagesList.length; i++) {
                if (pagesList[i].fullPath === fullPath) {
                    currentIndex = i
                }
            }
            if (currentIndex > -1 && pagesList.length > 0) {
                for (let i = 0; i < pagesList.length; i++) {
                    if (i > currentIndex) {
                        let thatPage = self.$store.getters.getPageByPath(pagesList[i].fullPath)
                        if (thatPage.fullVue && thatPage.fullVue.pageChange) {
                            self.jumpPage = false
                        } else {
                            self.$store.commit('destroyPage', pagesList[i].fullPath)
                            i = -1
                        }
                    }
                }
            }
            if (jumpPage) {
                let thatPage = this.$store.getters.getPageByPath(fullPath)
                if (thatPage) {
                    this.$routeTab(thatPage.fullPath, thatPage.query)
                }
            }
        },

        /**
         * 设置标签页标题
         * @param fullPath
         * @param param
         */
        $setTitle(fullPath, param) {
            param.fullPath = fullPath
            this.$store.commit('changePageTitle', param)
        },

        /**
         * 关闭页面
         * @param fullPath
         */
        closePage(fullPath) {
            if (this.pageClose) {
                if (!fullPath) fullPath = this.$route.fullPath
                this.$store.commit('destroyPage', fullPath)
            } else {
                this.$store.commit('hiddenPage', fullPath)
            }
        }
    },
    watch: {}
}
export default routerMethods
